//
//  LPTMessegeViewController.m
//  LPTClient
//
//  Created by mac on 2018/12/4.
//  Copyright © 2018年 zhaodd. All rights reserved.
//

#import "LPTMessegeViewController.h"
#import "LPTMessegeTableViewCell.h"
#import "LPTMessegeDetailViewController.h"
#import "LPTHomeMsgEntity.h"

@interface LPTMessegeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIImageView *indicator;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (assign, nonatomic) NSInteger selectIndex;
@property (strong, nonatomic) NSIndexPath *indexPath;

@end

@implementation LPTMessegeViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.dataSource = [NSMutableArray new];
    self.selectIndex = 0;
    
    [self initSubViews];
    
    [self requestWenzhenMsgList];
}

-(void)initSubViews
{
    self.tableView.tableFooterView = [UIView new];
    self.tableView.rowHeight = 67;
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        if (self.selectIndex == 0) {
            
            [self requestWenzhenMsgList];
            
        }else{
            
            //系统消息
            [self requestMsgList];
        }
    }];
    self.tableView.mj_header = header;
    
}

//问诊消息
- (IBAction)physicianMessegeAction:(id)sender {
    
    self.selectIndex = 0;
    [UIView animateWithDuration:0.2f animations:^{
        
        [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.indicator.superview).mas_offset(0);
        }];
        [self.view layoutIfNeeded];
        
    } completion:^(BOOL finished) {
        
    }];
    
    [self requestWenzhenMsgList];
}

//系统消息
- (IBAction)systemMessegeAction:(id)sender {
    
    self.selectIndex = 1;
    [UIView animateWithDuration:0.2f animations:^{
        
        [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.indicator.superview).mas_offset(kScreenWidth/2);
        }];
        [self.view layoutIfNeeded];
        
    } completion:^(BOOL finished) {
        
        
    }];
    
    [self requestMsgList];
    
}

//系统消息
-(void)requestMsgList
{
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:@"/api/system_message_list" withBody:@{@"page":@(1),@"size":@(1000)} withMethod:YTKRequestMethodGET];
    [request startRequestWithClass:[LPTHomeMsgListEntity class] completeSuccess:^(__kindof LPTBaseRequest * _Nullable request, LPTHomeMsgListEntity *data, NSInteger code) {
        
        [self.tableView.mj_header endRefreshing];
        
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:data.system_messages];
        [self.tableView reloadData];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
    }];
}

//问诊消息
-(void)requestWenzhenMsgList
{
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:@"/api/chat_all_new_message" withBody:@{} withMethod:YTKRequestMethodGET];
    
    [request startRequestCompleteSuccess:^(__kindof LPTBaseRequest * _Nullable request, id data, NSInteger code) {
        
        [self.tableView.mj_header endRefreshing];
        [self.dataSource removeAllObjects];
//        [self.dataSource addObjectsFromArray:data[@"messages"]];
        [self.tableView reloadData];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [self.tableView.mj_header endRefreshing];
        [self.dataSource removeAllObjects];
        [self.tableView reloadData];
        
        [MMProgressHUD show];
        [MMProgressHUD dismissWithError:data];
    }];
}

#pragma mark - UITableViewDataSource and Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LPTHomeMsgEntity *entity = self.dataSource[indexPath.row];
    LPTMessegeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LPTMessegeTableViewCell" forIndexPath:indexPath];
    [cell refreshWithMsgEntity:entity];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:nil];
    self.indexPath = indexPath;
    
    [self performSegueWithIdentifier:@"pushToMessageDetailIdentify" sender:nil];
}


#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
    if ([segue.identifier isEqualToString:@"pushToMessageDetailIdentify"]) {
        LPTHomeMsgEntity *entity = self.dataSource[self.indexPath.row];
        
        LPTMessegeDetailViewController *vc = segue.destinationViewController;
        vc.entity = entity;
    }
}


@end
